/*
 * JavaScript file
 */

var output;
var foreground;

function onhide() { foreground = false; /*document.getElementById('debug').innerHTML += "onhide()<br />"; */}
function onshow() { foreground = true;/*document.getElementById('debug').innerHTML += "onshow()<br />"; */}

function init()
{
	//	Add your code stpes here
	output = document.getElementById('output');
	NfcServer.onshow = onshow;
	NfcServer.onhide = onhide;
}

function readTag()
{
	output.innerHTML = "reading";
	NfcServer.readTag(NfcServer.NdefRecordTnf.WellKnown, "Sp", 2,
		function(text)
		{
			if (text == "Timed out")
			{
				if (foreground)
				{
					readTag();
				}
				else
				{
					output.innerHTML = "timed out";
				}
			}
			else
			{
				var escapedXml = text.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
				output.innerHTML = "<p><b>Read Tag:</b></br>" + escapedXml;
			}
		},
		function (err)
		{
		    output.innerHTML = "failed";
			output.innerHTML = "Error: "+ err.status + ". The APIBridge returned error: "+err.responseText+"<br/>";
		}
	);
}

